﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace VincentDAL
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {            
            string db_host = tbServer.Text.Trim();
            string db_user = tbUserid.Text.Trim();
            string db_password = tbPassword.Text.Trim();
            string db_name = tbDatabase.Text.Trim();
            string db_namespace = tbNameSpace.Text.Trim();
            string db_auther = tbAuther.Text.Trim();
            
            if (db_name == "")
            {
                MessageBox.Show("please input Database!");
                tbDatabase.Focus();

                return;
            }

            if (db_user == "")
            {
                MessageBox.Show("please input User ID!");
                tbDatabase.Focus();

                return;
            }

            // 清除提示信息
            COutputMessage.Clear(rtb);

            // 连接数据库
            using (CDatabase db = new CDatabase())
            {
                string outmsg = "";

                if (db.isConnection(db_host, db_user, db_password, db_name))
                {
                    COutputMessage.Show(rtb, "生成成功!");
                }
                else
                {
                    COutputMessage.Show(rtb, "生成失败!");
                    return;
                }                

                // 删除原有文件
                CFile.DeleteDirectory(CUtility.GetBaseDirectory() + db_name + "\\");   

                // 查询所有表
                DataTable dt = db.ExecuteSql("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'");

                // 生成常用sql,保存在文件 Script.sql 中
                outmsg = CAutoSql.CreateSql(dt, db_name, db);
                COutputMessage.Show(rtb, outmsg);
                
                // 根据表名查询出表结构                   
                string tablename = "";
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    tablename = dt.Rows[i]["TABLE_NAME"].ToString();

                    if (tablename.Equals(""))
                        continue;

                    // 生成常用存储过程sql
                    outmsg = CAutoSql.CreateSql(db_name, tablename, db);
                    COutputMessage.Show(rtb, outmsg);
                    
                    // 生成模型
                    outmsg = CAutoModel.CreateModel(db_name, tablename, db_namespace, db_auther, db);
                    COutputMessage.Show(rtb, outmsg);

                    // 生成数据持久层
                    outmsg = CAutoDAL.CreateDAL(db_name, tablename, db_namespace, db_auther, db);
                    COutputMessage.Show(rtb, outmsg);

                    // 生成业务层
                    outmsg = CAutoBLL.CreateBLL(db_name, tablename, db_namespace, db_auther, db);
                    COutputMessage.Show(rtb, outmsg);
                }                                

            }
        }

        /// <summary>
        /// 测试数据库连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTestConnection_Click(object sender, EventArgs e)
        {
            string db_host = tbServer.Text.Trim();
            string db_user = tbUserid.Text.Trim();
            string db_password = tbPassword.Text.Trim();
            string db_name = tbDatabase.Text.Trim();

            // 连接数据库
            using (CDatabase db = new CDatabase())
            {
                if (db.isConnection(db_host, db_user, db_password, db_name))
                    MessageBox.Show("连接成功！");
                else
                    MessageBox.Show("连接失败！");
            }
        }

    }
}
